<?php
global $wpdb;

if (isset($_POST['sync'])) {
	
	$urlSync = HFR_SERVER . "/rest/sync-get-source?c=".get_option("hfr_customer")."&k=".get_option("hfr_license_key")."&b=".get_option("hfr_blog_url") ;
	$content = hfr_remote_get($urlSync);
	
	if ($content != null && $content != "") {
		$result = json_decode($content);
		
		if ($result->status == "error") {
			echo $result->message;
		} else {
			
			$message = "Sync success ";
			
			//Remove all source sitemap
			$wpdb->query("DELETE FROM ".$wpdb->prefix."hfr_url_craw");
			
			foreach ($result->urlCraws as $k => $url) {
				
				$row = $wpdb->query( $wpdb->prepare(
					"
					INSERT INTO ".$wpdb->prefix."hfr_url_craw
					( 
						`id`, `sitemap`, `userBlog`,
						`blog`, `reglink`, `type`,
						`domain`, `tag`, `linkSelecterJs`,
						`activeLinkJs`, `linkSelecter`, `titleSelecter`,
						`contentSelecter`, `removeSelecter`, `customSelecter`,
						`category`, `typelink`, `source`,
						`sourcetype`, `source_image`, `remove_tag_a`,
						`activeContentJs`, `md5`, `timeCraw`, 
						`error`, `typeError`, `timeError`,
						`time`, `active`, `field`,
						`order`, `share`, `craw`,
						`getAll`, `arrLink`
					)
					VALUES ( 
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %s,
					%s, %s, %d,
					%s, %s, %f,
					%f, %s, %s,
					%s, %s, %s,
					%s, %s
					 )
					",
					$url->id, $url->sitemap, $url->userBlog,
					$url->blog, $url->reglink, $url->type,
					$url->domain, $url->tag, $url->linkSelecterJs,
					$url->activeLinkJs, $url->linkSelecter, $url->titleSelecter,
					$url->contentSelecter, $url->removeSelecter, $url->customSelecter,
					$url->category, $url->typelink, $url->source,
					$url->sourcetype, $url->source_image, $url->remove_tag_a,
					$url->activeContentJs, $url->md5, $url->timeCraw,
					$url->error, $url->typeError, $url->timeError,
					$url->time, $url->active, $url->field,
					$url->order, $url->share, $url->craw,
					$url->getAll, $url->arrLink
				) );
			}
			
			$message = $message . count($result->urlCraws)." rows sitemap";
			
			//Remove all payment
			$wpdb->query("DELETE FROM ".$wpdb->prefix."hfr_payment");
			
			foreach ($result->restPayments as $k => $rest) {
				$row = $wpdb->query( $wpdb->prepare(
					"
					INSERT INTO ".$wpdb->prefix."hfr_payment
					( 
						`id`, `customer`, `price`,
						`content`, `created`, `start`,
						`end`
					)
					VALUES ( 
					%s, %s, %d,
					%s, %s, %s,
					%s
					 )
					",
					$rest->id, $rest->customer, $rest->price,
					$rest->content, hfr_getdate($rest->created, "Y-m-d h:i:s"), hfr_getdate($rest->start, "Y-m-d h:i:s"),
					hfr_getdate($rest->end, "Y-m-d h:i:s")
				) );
			}
			$message = $message. ", " .count($result->restPayments)." rows payment <br />";
			?>
			<div class="hfr-alert-success"><?=$message ?></div>
			<?php
		}
		 
	} else {
		hfr_debug($content);
	}
	/*if ($content != null && isset($content['body'])) {
		print_r($content);
	} else {
		echo "url: ".$urlSync."<br />";
		echo "Sync error.";
	}*/
	
	
}
?>
<?php echo "<h2>". __("Sync Data", "hfr_dom") . "</h2>";?>
<form action="admin.php?page=hfr_source_manager&act=sync" method="post">
	<input type="hidden" name="sync" value="1">
	<p class="submit">
	<input type="submit" name="Submit" value="<?php _e('Sync Data') ?>" />
	</p>
</form>